//
//  BTManager.h
//  BMS
//
//  Created by 颜魏 on 2021/4/16.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

typedef NS_ENUM(NSUInteger, BTWriteType) {
    BTWriteTypeEnterDebug = 0,
    BTWriteTypeCancelDebug,
    BTWriteTypeReadData,
    BTWriteTypeGain,    /**< 一些指令的设置:例如读取上次时间,设置当前时间等等 */
};

//扫描发现设备回调
typedef void (^BTManagerDiscoverPeripheralCallBack) (NSArray *peripherals);
//连接设备回调
typedef void (^BTManagerConnectPeripheralCallBack) (BOOL isConnected);
//发送数据回调
typedef void (^BTManagerWriteDataCallBack) (BTWriteType writeType, NSString *readStr);
//主动断开连接成功后的回调
typedef void(^BTManagerDisconnectPeripheralCallBlock)(void);


@interface BTManager : NSObject

@property (strong, nonatomic) CBPeripheral     *peripheral;     //外设-蓝牙硬件
@property (strong, nonatomic) NSMutableArray    *peripherals;   // 发现的所有 硬件设备

@property (nonatomic,assign) BOOL             isConnected;   //连接成功= yes，失败=no
@property (nonatomic,assign) BOOL             isAutoDisconnect;    //是否自动连接，是=yes，不=no
@property (atomic, assign) BOOL           connectStatu;// 蓝牙连接状态
@property (assign, nonatomic) BOOL isScaning; //是否正在扫描 是=yes，没有=no
@property (nonatomic, assign) BOOL isInitiativeDis; //是否是主动断开
@property (nonatomic, assign) BTWriteType bleType; /**< 记录是读取数据还是调试 */

@property (copy, nonatomic) BTManagerConnectPeripheralCallBack connectBlock;    //连接蓝牙回调
@property (copy, nonatomic) BTManagerDiscoverPeripheralCallBack scanBlock;  //发现设备回调
@property (nonatomic,assign) BOOL             isGettingBattery;   //是否正在查询电量
@property (strong, nonatomic) NSMutableDictionary    *RSSIDict; // 蓝牙信号数组
@property (nonatomic, strong) NSMutableArray *batteryLevelSource;
@property (strong, nonatomic) NSArray<CBPeripheral *>    *lookSocBlues;   // 需要查询电量的 硬件设备

@property (nonatomic, assign) NSInteger type;

@property (nonatomic, copy) NSString *logStr;

#pragma mark - method
/// 单例方法
+ (instancetype)sharedBTManager;

/// 获取手机蓝牙状态
- (BOOL)isLECapableHardware;

/// 开启蓝牙扫描
- (void)startScan;

/// 开始扫描并在scanInterval秒后停止
/// @param scanInterval 时间s
/// @param callBack 完成回调
- (void)startScanWithInterval:(NSInteger)scanInterval completion:(BTManagerDiscoverPeripheralCallBack)callBack;

/// 停止扫描
- (void)stopScan;

/// 连接到指定设备
/// @param peripheral 设备
- (void)connectPeripheral:(CBPeripheral *)peripheral;

/// 连接到指定设备，并回调
/// @param peripheral 设备
/// @param callBack 回调
- (void)connectPeripheral:(CBPeripheral *)peripheral completion:(BTManagerConnectPeripheralCallBack)callBack;

/// 尝试重新连接
- (void)reConnectPeripheral;

/// 尝试重新连接
/// @param peripheral 设备
- (void)reConnectPeripheral:(CBPeripheral *)peripheral;

/// 断开连接
/// @param peripheral 设备
- (void)disconnectPeripheral:(CBPeripheral *)peripheral;

/// 断开现有设备的重新扫描
- (void)retartScan;

/// 断开现连设备
-(void)disconnectRootPeripheral;

/// 断开现连设备
/// @param callBlock 回调
- (void)disconnectRootPeripheral:(BTManagerDisconnectPeripheralCallBlock)callBlock;

/// 发送通用指令
/// @param commandData 指令
/// @param length 长度
- (void)sendCommand:(Byte*)commandData length:(int)length;

/// 发送数据
/// @param data 数据
/// @param encoding 编码
- (BOOL)writeData:(NSData *)data encoding:(NSStringEncoding )encoding;


#pragma mark - FX 智能笔数据处理 ******
-(void)sendCommandWithCrc:(Byte*)commandData length:(int)length;

@end

